package com.young.shop.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;

public class DBUtil {

    //数据源
    private static DataSource dataSource;
    //QueryRunner对象
    private static QueryRunner queryRunner;

    //初始化一个连接池
    static {
        InputStream resourceAsStream = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");

        Properties properties = new Properties();

        try {
            properties.load(resourceAsStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

        queryRunner = new QueryRunner(dataSource);

    }



    public static DataSource getDataSource() {
        return dataSource;
    }

    public static QueryRunner getQueryRunner() {
        return queryRunner;
    }
}